

function(conan_message MESSAGE_OUTPUT)
    if(NOT CONAN_CMAKE_SILENT_OUTPUT)
        message(${ARGV${0}})
    endif()
endfunction()


macro(conan_find_apple_frameworks FRAMEWORKS_FOUND FRAMEWORKS FRAMEWORKS_DIRS)
    if(APPLE)
        foreach(_FRAMEWORK ${FRAMEWORKS})
            # https://cmake.org/pipermail/cmake-developers/2017-August/030199.html
            find_library(CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND NAMES ${_FRAMEWORK} PATHS ${FRAMEWORKS_DIRS} CMAKE_FIND_ROOT_PATH_BOTH)
            if(CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND)
                list(APPEND ${FRAMEWORKS_FOUND} ${CONAN_FRAMEWORK_${_FRAMEWORK}_FOUND})
            else()
                message(FATAL_ERROR "Framework library ${_FRAMEWORK} not found in paths: ${FRAMEWORKS_DIRS}")
            endif()
        endforeach()
    endif()
endmacro()


function(conan_package_library_targets libraries package_libdir deps out_libraries out_libraries_target build_type package_name)
    unset(_CONAN_ACTUAL_TARGETS CACHE)
    unset(_CONAN_FOUND_SYSTEM_LIBS CACHE)
    foreach(_LIBRARY_NAME ${libraries})
        find_library(CONAN_FOUND_LIBRARY NAMES ${_LIBRARY_NAME} PATHS ${package_libdir}
                     NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
        if(CONAN_FOUND_LIBRARY)
            conan_message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
            list(APPEND _out_libraries ${CONAN_FOUND_LIBRARY})
            if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
                # Create a micro-target for each lib/a found
                string(REGEX REPLACE "[^A-Za-z0-9.+_-]" "_" _LIBRARY_NAME ${_LIBRARY_NAME})
                set(_LIB_NAME CONAN_LIB::${package_name}_${_LIBRARY_NAME}${build_type})
                if(NOT TARGET ${_LIB_NAME})
                    # Create a micro-target for each lib/a found
                    add_library(${_LIB_NAME} UNKNOWN IMPORTED)
                    set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_FOUND_LIBRARY})
                    set(_CONAN_ACTUAL_TARGETS ${_CONAN_ACTUAL_TARGETS} ${_LIB_NAME})
                else()
                    conan_message(STATUS "Skipping already existing target: ${_LIB_NAME}")
                endif()
                list(APPEND _out_libraries_target ${_LIB_NAME})
            endif()
            conan_message(STATUS "Found: ${CONAN_FOUND_LIBRARY}")
        else()
            conan_message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
            list(APPEND _out_libraries_target ${_LIBRARY_NAME})
            list(APPEND _out_libraries ${_LIBRARY_NAME})
            set(_CONAN_FOUND_SYSTEM_LIBS "${_CONAN_FOUND_SYSTEM_LIBS};${_LIBRARY_NAME}")
        endif()
        unset(CONAN_FOUND_LIBRARY CACHE)
    endforeach()

    if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
        # Add all dependencies to all targets
        string(REPLACE " " ";" deps_list "${deps}")
        foreach(_CONAN_ACTUAL_TARGET ${_CONAN_ACTUAL_TARGETS})
            set_property(TARGET ${_CONAN_ACTUAL_TARGET} PROPERTY INTERFACE_LINK_LIBRARIES "${_CONAN_FOUND_SYSTEM_LIBS};${deps_list}")
        endforeach()
    endif()

    set(${out_libraries} ${_out_libraries} PARENT_SCOPE)
    set(${out_libraries_target} ${_out_libraries_target} PARENT_SCOPE)
endfunction()


include(FindPackageHandleStandardArgs)

conan_message(STATUS "Conan: Using autogenerated FindSDL2_net.cmake")
# Global approach
set(SDL2_net_FOUND 1)
set(SDL2_net_VERSION "2.2.0")

find_package_handle_standard_args(SDL2_net REQUIRED_VARS
                                  SDL2_net_VERSION VERSION_VAR SDL2_net_VERSION)
mark_as_advanced(SDL2_net_FOUND SDL2_net_VERSION)


set(SDL2_net_INCLUDE_DIRS "${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/include"
			"${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/include/SDL2")
set(SDL2_net_INCLUDE_DIR "${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/include;${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/include/SDL2")
set(SDL2_net_INCLUDES "${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/include"
			"${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/include/SDL2")
set(SDL2_net_RES_DIRS )
set(SDL2_net_DEFINITIONS )
set(SDL2_net_LINKER_FLAGS_LIST
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:>"
        "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>"
)
set(SDL2_net_COMPILE_DEFINITIONS )
set(SDL2_net_COMPILE_OPTIONS_LIST "" "")
set(SDL2_net_COMPILE_OPTIONS_C "")
set(SDL2_net_COMPILE_OPTIONS_CXX "")
set(SDL2_net_LIBRARIES_TARGETS "") # Will be filled later, if CMake 3
set(SDL2_net_LIBRARIES "") # Will be filled later
set(SDL2_net_LIBS "") # Same as SDL2_net_LIBRARIES
set(SDL2_net_SYSTEM_LIBS ws2_32 iphlpapi)
set(SDL2_net_FRAMEWORK_DIRS )
set(SDL2_net_FRAMEWORKS )
set(SDL2_net_FRAMEWORKS_FOUND "") # Will be filled later
set(SDL2_net_BUILD_MODULES_PATHS )

conan_find_apple_frameworks(SDL2_net_FRAMEWORKS_FOUND "${SDL2_net_FRAMEWORKS}" "${SDL2_net_FRAMEWORK_DIRS}")

mark_as_advanced(SDL2_net_INCLUDE_DIRS
                 SDL2_net_INCLUDE_DIR
                 SDL2_net_INCLUDES
                 SDL2_net_DEFINITIONS
                 SDL2_net_LINKER_FLAGS_LIST
                 SDL2_net_COMPILE_DEFINITIONS
                 SDL2_net_COMPILE_OPTIONS_LIST
                 SDL2_net_LIBRARIES
                 SDL2_net_LIBS
                 SDL2_net_LIBRARIES_TARGETS)

# Find the real .lib/.a and add them to SDL2_net_LIBS and SDL2_net_LIBRARY_LIST
set(SDL2_net_LIBRARY_LIST SDL2_net)
set(SDL2_net_LIB_DIRS "${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/lib")

# Gather all the libraries that should be linked to the targets (do not touch existing variables):
set(_SDL2_net_DEPENDENCIES "${SDL2_net_FRAMEWORKS_FOUND} ${SDL2_net_SYSTEM_LIBS} SDL2::SDL2")

conan_package_library_targets("${SDL2_net_LIBRARY_LIST}"  # libraries
                              "${SDL2_net_LIB_DIRS}"      # package_libdir
                              "${_SDL2_net_DEPENDENCIES}"  # deps
                              SDL2_net_LIBRARIES            # out_libraries
                              SDL2_net_LIBRARIES_TARGETS    # out_libraries_targets
                              ""                          # build_type
                              "SDL2_net")                                      # package_name

set(SDL2_net_LIBS ${SDL2_net_LIBRARIES})

foreach(_FRAMEWORK ${SDL2_net_FRAMEWORKS_FOUND})
    list(APPEND SDL2_net_LIBRARIES_TARGETS ${_FRAMEWORK})
    list(APPEND SDL2_net_LIBRARIES ${_FRAMEWORK})
endforeach()

foreach(_SYSTEM_LIB ${SDL2_net_SYSTEM_LIBS})
    list(APPEND SDL2_net_LIBRARIES_TARGETS ${_SYSTEM_LIB})
    list(APPEND SDL2_net_LIBRARIES ${_SYSTEM_LIB})
endforeach()

# We need to add our requirements too
set(SDL2_net_LIBRARIES_TARGETS "${SDL2_net_LIBRARIES_TARGETS};SDL2::SDL2")
set(SDL2_net_LIBRARIES "${SDL2_net_LIBRARIES};SDL2::SDL2")

set(CMAKE_MODULE_PATH "${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/" ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH "${PROJECT_3RDPARTY_DIR}/sdl_net-2.2.0/" ${CMAKE_PREFIX_PATH})

if(NOT ${CMAKE_VERSION} VERSION_LESS "3.0")
    # Target approach
    if(NOT TARGET SDL2_net::SDL2_net)
        add_library(SDL2_net::SDL2_net INTERFACE IMPORTED)
        if(SDL2_net_INCLUDE_DIRS)
            set_target_properties(SDL2_net::SDL2_net PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                                  "${SDL2_net_INCLUDE_DIRS}")
        endif()
        set_property(TARGET SDL2_net::SDL2_net PROPERTY INTERFACE_LINK_LIBRARIES
                     "${SDL2_net_LIBRARIES_TARGETS};${SDL2_net_LINKER_FLAGS_LIST}")
        set_property(TARGET SDL2_net::SDL2_net PROPERTY INTERFACE_COMPILE_DEFINITIONS
                     ${SDL2_net_COMPILE_DEFINITIONS})
        set_property(TARGET SDL2_net::SDL2_net PROPERTY INTERFACE_COMPILE_OPTIONS
                     "${SDL2_net_COMPILE_OPTIONS_LIST}")
        
        # Library dependencies
        include(CMakeFindDependencyMacro)

        if(NOT SDL2_FOUND)
            find_dependency(SDL2 REQUIRED)
        else()
            message(STATUS "Dependency SDL2 already found")
        endif()

    endif()
endif()

foreach(_BUILD_MODULE_PATH ${SDL2_net_BUILD_MODULES_PATHS})
    include(${_BUILD_MODULE_PATH})
endforeach()
